<?php

namespace app\api\service;

use think\facade\Db;

class TaskService
{
    /**
     * 获取店铺任务列表
     * */
    public static function getTaskList($store_id)
    {
        $taskList = Db::name('activity_task')
            ->alias('a')
            ->field('a.id,a.task_type,b.activity_title,a.time_len,c.combo_name,a.join_num,a.expire_type,a.expire_time,a.start_time,a.end_time,a.give_integral')
            ->leftjoin('group_activity b', 'a.group_activity_id=b.id')
            ->leftjoin('combo c', 'a.user_combo_id=c.id')
            ->withAttr('start_time', function ($value) {
                if (empty($value)) {
                    return $value;
                }
                return date('Y-m-d H:i:s', $value);
            })->withAttr('end_time', function ($value) {
            if (empty($value)) {
                return $value;
            }
            return date('Y-m-d H:i:s', $value);
        })->where(['a.store_id' => $store_id, 'a.status' => 1])
            ->select();

        return $taskList;
    }

    /**
     * 获取任务统计
     * */
    public static function getTaskTotalInfo($user_id)
    {
        $total_task_num = Db::name('user_task')->where('user_id', $user_id)->count('id');
        $starting_task_num = Db::name('user_task')->where(['user_id' => $user_id, 'task_status' => 1])->count('id');
        $finish_task_num = Db::name('user_task')->where(['user_id' => $user_id, 'task_status' => 2])->count('id');

        $user = Db::name('user')->where('user_id', $user_id)->field('nickname,avatar,integral')->find();

        return [
            'total_task_num' => $total_task_num,
            'starting_task_num' => $starting_task_num,
            'finish_task_num' => $finish_task_num,
            'user' => $user,
        ];
    }

    public static function getUserTaskList($user_id, $task_status, $store_id)
    {
        $query = Db::name('user_task')
            ->alias('a')
            ->field('a.id,a.task_type,b.activity_title,c.combo_name,a.join_num,a.finish_join_num,a.start_time,a.end_time,a.task_status,a.give_integral')
            ->leftjoin('group_activity b', 'a.group_activity_id=b.id')
            ->leftjoin('combo c', 'a.user_combo_id=c.id')
            ->withAttr('start_time', function ($value) {
                if (empty($value)) {
                    return $value;
                }
                return date('Y-m-d H:i:s', $value);
            })->withAttr('end_time', function ($value) {
            if (empty($value)) {
                return $value;
            }
            return date('Y-m-d H:i:s', $value);
        })->where('a.user_id', $user_id)
            ->order(['a.end_time' => 'asc', 'a.id' => 'desc']);

        if (!empty($task_status)) {
            $query->where('a.task_status', $task_status);
        }

        $userTaskList = $query->paginate(10);

        return $userTaskList;
    }

    public static function receiveTask($user_id, $task)
    {

        if ($task['expire_type'] == 1) {
            $time = time();
            $start_time = $time;
            $end_time = $time + $task['expire_time'] * 3600;
        }

        if ($task['expire_type'] == 2) {
            $start_time = $task['start_time'];
            $end_time = $task['end_time'];
        }

        $result = Db::name('user_task')->insert([
            'user_id' => $user_id,
            'task_id' => $task['id'],
            'task_type' => $task['task_type'],
            'join_num' => $task['join_num'],
            'user_combo_id' => $task['user_combo_id'],
            'group_activity_id' => $task['group_activity_id'],
            'start_time' => $start_time,
            'end_time' => $end_time,
            'give_integral' => $task['give_integral'],
            'give_coupon' => $task['give_coupon'],
        ]);

        return $result;
    }

    /**
     * 获取任务详情
     * */
    public static function findTaskDetail($task_id)
    {
        $task = Db::name('activity_task')
            ->field('id,task_type,join_num,give_integral,give_coupon,user_combo_id,group_activity_id,start_time,end_time,expire_type,expire_time')
            ->where('id', $task_id)
            ->find();

        if (empty($task)) {
            return [];
        }

        if ($task['expire_type'] == 2 && $task['end_time'] < time()) {
            return [];
        }

        return $task;
    }

}
